package com.hdcx.Minfresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.bean.MoneyWrapper;
import com.hdcx.Minfresh.bean.UserInfoBean;
import com.hdcx.Minfresh.url.MyUrl;
import com.hdcx.Minfresh.url.RequstCode;
import com.hdcx.Minfresh.util.AppUtil;
import com.hdcx.Minfresh.util.ShpfUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/11/18.
 */

public class MyMoneyActivity extends BaseActivity {
    TextView tv_money;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_yu_e);
        initView();
        initData();
    }

    private void initView() {
        findViewById(R.id.bt_cz).setOnClickListener(this);
        TextView title = (TextView) findViewById(R.id.tv_title);
        title.setText("我的钱包");
        findViewById(R.id.tv_left).setOnClickListener(this);
        tv_money = (TextView) findViewById(R.id.tv_money);
        TextView tv_right =  (TextView) findViewById(R.id.tv_right);
        tv_right.setText("账户记录");
        tv_right.setOnClickListener(this);
    }
    private void initData() {
        post_main();
    }

    /*参数
            uid		//用户id 20*/
    private void post_main() {
        JSONObject json = new JSONObject();
        AppUtil.showLoading(this);
        UserInfoBean user =  ShpfUtil.getObject(ShpfUtil.LOGIN);
        try {
            json.put("uid", user.getUserinfo().getId());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.postString()
                .url(MyUrl.USERMONEY)
                .content(json.toString())
                .build()
                .execute(new Callback<MoneyWrapper>() {
                    @Override
                    public MoneyWrapper parseNetworkResponse(Response response) throws IOException {
                        String json = response.body().string();
                        return new Gson().fromJson(json, MoneyWrapper.class);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                    }

                    @Override
                    public void onResponse(MoneyWrapper response) {
                        AppUtil.closeLoading();
                        if (response.getStatus() == 1) {
                            setValue(response);
                        }else {

                        }
                    }
                });
    }

    private void setValue(MoneyWrapper response) {
        tv_money.setText(response.getAccount());

    }


    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()){
           case R.id.tv_right:
               intent.setClass(MyMoneyActivity.this,MoneyDetailActivity.class);
               startActivity(intent);
               break;
           case R.id.bt_cz:
               intent.setClass(this,ChongzhiActivity.class);
               intent.putExtra("money",tv_money.getText().toString());
               startActivityForResult(intent, RequstCode.CHONGZHI);
               break;
           case R.id.tv_left:
               finish();
               break;
       }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            if(requestCode==RequstCode.CHONGZHI){
                post_main();
            }

    }
}
